package kr.hkit.summart_java.serviceUI;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.awt.GridLayout;

import javax.swing.JLabel;

import kr.hkit.myframework.uicomp.InputPanel;
import kr.hkit.summart_java.dao.DaoInterface;
import kr.hkit.summart_java.erp.dto.Department;

import java.awt.BorderLayout;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.BoxLayout;

import java.awt.FlowLayout;
import java.sql.SQLException;

public class PanelDepartmentUI extends JPanel implements ActionListener {

	private InputPanel pDeptNo;
	private InputPanel pDeptName;
	private InputPanel pDeptFloor;
	private DaoInterface<Department> dao;
	private JLabel label;

	/**
	 * Create the panel.
	 */
	public PanelDepartmentUI() {

		initialize();
	}
	private void initialize() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel_Center = new JPanel();
		add(panel_Center);
		panel_Center.setLayout(new GridLayout(0, 2, 0, 0));
		
		pDeptNo = new InputPanel("부서번호");
		panel_Center.add(pDeptNo);
		pDeptNo.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		pDeptName = new InputPanel("부서명");
		panel_Center.add(pDeptName);
		pDeptName.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		pDeptFloor = new InputPanel("위치");
		panel_Center.add(pDeptFloor);
		pDeptFloor.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		label = new JLabel("");
		panel_Center.add(label);
		
		JPanel panel_Bottom = new JPanel();
		add(panel_Bottom, BorderLayout.SOUTH);
		
		JButton btnadd = new JButton("추가");
		btnadd.addActionListener(this);
		panel_Bottom.add(btnadd);
	}

	public void actionPerformed(ActionEvent e) {
		int dno = Integer.parseInt(pDeptNo.getTf().trim());
		String name = pDeptName.getTf();
		int floor =Integer.parseInt(pDeptFloor.getTf().trim());
		
		Department d = new Department(dno, name, floor);
		JOptionPane.showMessageDialog(this, d);
		try {
			dao.insertItem(d);
			JOptionPane.showMessageDialog(this, "입력되었습니다.");
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "입력실패.");
		}
	}

	public void setDao(DaoInterface<Department> dao) {
		this.dao = dao;
	}
}
